﻿$(document).ready(function () {
    assignButtons();
});

function assignButtons() {
    $(".linkButton").click(postForm);
}

function postForm(e) {
    var pathname = window.location.pathname;
    $form = $(this).closest("form");
    var postData = $form.serializeObject();
    
    $.ajax({
        type: "POST",
        url: pathname + "/" + $(this).attr("href").substring(1),
        data: "{'postData':" + JSON.stringify(postData) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $.each(msg.d, function (key, value) {
                switch($("#" + key).get(0).tagName)
                {
                    case 'INPUT':
                        $("#" + key).val(value);
                        break;
                    case 'LABEL':
                    case 'SPAN':
                        $("#" + key).html(value);
                        break;
                } 
            });
        }
        
    });

    e.preventDefault();
}

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};